package example.servlet;

import grpc.htsoft.helloworld.*;
import io.grpc.servlet.ServletAdapter;
import io.grpc.servlet.ServletServerBuilder;
import io.grpc.stub.StreamObserver;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpServlet;

import java.io.IOException;

//@WebServlet(name = "TestServlet", value = "/test")
//@WebServlet(urlPatterns = {"/helloworld.Greeter/SayHello"}, asyncSupported = true)
@WebServlet(urlPatterns = {"/helloworld.Greeter/*"}, asyncSupported = true)
//@WebServlet(urlPatterns = {"/"}, asyncSupported = true)
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

//    private final ServletAdapter servletAdapter =
//            new ServletServerBuilder().addService(new GreeterImpl()).buildServletAdapter();

    private static final class GreeterImpl extends GreeterGrpc.GreeterImplBase {
        GreeterImpl() {}

        @Override
        public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
            System.out.println("sayhello");
            HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).setCode("code aa").build();
            responseObserver.onNext(reply);
            responseObserver.onCompleted();



        }

        @Override
        public void getUser(User request, StreamObserver<UserReply> responseObserver) {
            System.out.println("getUser");
            UserReply reply = UserReply.newBuilder().setName("getUser " + request.getName()).build();
            responseObserver.onNext(reply);
            responseObserver.onCompleted();

        }

        @Override
        public void getUser2(User request, StreamObserver<User> responseObserver) {
            super.getUser2(request, responseObserver);
        }

        @Override
        public void sayHelloStreamReply(HelloRequest request, StreamObserver<HelloReply> responseObserver) {
            super.sayHelloStreamReply(request, responseObserver);
        }

        @Override
        public void getDataSet(User request, StreamObserver<DataSet> responseObserver) {
            System.out.println("getDataSet");
            Cell c = Cell.newBuilder().setKey("name").setValue("zgw1 server").build();
            Cell c2 = Cell.newBuilder().setKey("sex").setValue("man").build();
            Row r = Row.newBuilder().addCells(c).addCells(c2).build();

            Cell c3 = Cell.newBuilder().setKey("name").setValue("zgw2 server").build();
            Cell c4 = Cell.newBuilder().setKey("sex").setValue("feman").build();
            Row r2 = Row.newBuilder().addCells(c3).addCells(c4).build();
            DataSet d = DataSet.newBuilder().addRows(r).addRows(r2).build();
            responseObserver.onNext(d);
            responseObserver.onCompleted();

        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        response.setContentType("text/html");
        response.getWriter().println("<p>Hello World!</p>");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        System.out.println("dopost");
//        if (ServletAdapter.isGrpc(request))
        if (isGrpc(request))
        {
            System.out.println("dogrpc");
//            AsyncContext asyncContext = request.startAsync();
            // 异步操作的其他逻辑
//            asyncContext.complete();
//            servletAdapter.doPost(request, response);
        } else {
            System.out.println(" not dogrpc");
            response.setContentType("text/html");
            response.getWriter().println("<p>Hello non-gRPC2 client!</p>");
        }
    }


    @Override
    public void destroy() {
//        servletAdapter.destroy();
        super.destroy();
    }

    private boolean isGrpc(HttpServletRequest request) {
        String contentType = request.getContentType();
        return contentType != null && contentType.startsWith("application/grpc");
    }
}
